package com.study.book.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.book.common.R;
import com.study.book.dto.OrderDto;
import com.study.book.mapper.LibraryMapper;
import com.study.book.mapper.ProductMapper;
import com.study.book.pojo.Order;
import com.study.book.service.OrderService;
import com.study.book.mapper.OrderMapper;
import com.study.book.utils.RandomUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author ASUS
* @description 针对表【Order】的数据库操作Service实现
* @createDate 2024-05-05 18:18:07
*/
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order>
    implements OrderService{

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private LibraryMapper libraryMapper;
    @Autowired
    private ProductMapper productMapper;
    @Override
    public List<Order> getOrderListForUserId(int id) {
        return orderMapper.selectList(new LambdaQueryWrapper<Order>()
                .eq(Order::getAdminid,id)
                .orderByDesc(Order::getCommittime));
    }


    @Override
    public String orderReview(OrderDto dto) {
        Order order = orderMapper.selectOne(new LambdaQueryWrapper<Order>()
                .eq(Order::getOrdernumber, dto.getOrdernumber()));
        order.setComment(dto.getComment());
        order.setStar(Integer.valueOf(dto.getStar()));
        order.setStatus("2");//已完成
        orderMapper.updateById(order);
        return "评论成功";
    }

    @Override
    public R creatOrder(String id,OrderDto dto) {
        Order order = new Order();
        BeanUtils.copyProperties(dto,order);
        order.setAdminid(Integer.parseInt(id));
        order.setCommittime(new Date());
        order.setStatus("1");//待评价
        order.setQuantity(Integer.valueOf(dto.getQuantity()));
        order.setOrdernumber(RandomUtil.getRandomNickname(7));//订单随机数
        orderMapper.insert(order);
        return R.ok("购买成功");
    }


}




